package com.ruoyi.project.hcy.product.controller;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.hcy.product.domain.specification.ProSpecification;
import com.ruoyi.project.hcy.product.service.IProSpecificationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ProSpecificationController
 * @Author SAI
 * @Date 2020/9/18 13:57
 * @Description ProSpecificationController
 * @Version 1.0
 */
@Controller
@RequestMapping("/hcy/product/proSpecification")
public class ProSpecificationController extends BaseController {

    @Autowired
    private IProSpecificationService proSpecificationService;


    private String prefix = "hcy/product/specification";

    /**
     * 访问菜单
     * @return
     */
    @RequiresPermissions("product:proSpecification:view")
    @GetMapping()
    public String operlog(){
        return prefix + "/proSpecification";
    }

    /**
     * 查询规格集合，包含分页和条件查询
     * @param proSpecification
     * @return
     */
    @RequiresPermissions("product:proSpecification:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProSpecification proSpecification){
        startPage();
        List<ProSpecification> list = proSpecificationService.selectProSpecificationList(proSpecification);
        return getDataTable(list);
    }


    /**
     * 新增面的转发
     * @return
     */
    @GetMapping("/add")
    public String add(){
        return prefix +"/add";
    }


    /**
     * 新增保存规格
     * @param proSpecification
     * @return
     */
    @RequiresPermissions("product:proSpecification:add")
    @Log(title = "产品规格管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProSpecification proSpecification){
        if ("1".equals(proSpecificationService.checkSpecificationNameUnique(proSpecification))) {
            return error("新增规格'" + proSpecification.getSpecificationName() + "'失败，规格名称已存在");
        }
        return toAjax(proSpecificationService.insertProSpecification(proSpecification));
    }


    /**
     * 查询规格名称是否唯一
     * @param proSpecification
     * @return
     */
    @PostMapping("/checkSpecificationNameUnique")
    @ResponseBody
    public String checkSpecificationNameUnique(ProSpecification proSpecification) {
        return proSpecificationService.checkSpecificationNameUnique(proSpecification);
    }


    /**
     * 修改规格页面数据回显
     * @param specificationId
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{specificationId}")
    public String edit(@PathVariable("specificationId") Long specificationId, ModelMap mmap){
        mmap.put("specification",proSpecificationService.selectProSpecificationById(specificationId));
        return prefix+"/edit";
    }

    /**
     * 修改保存规格
     * @param proSpecification
     * @return
     */
    @RequiresPermissions("product:proSpecification:edit")
    @Log(title = "产品规格管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProSpecification proSpecification){
        if ("1".equals(proSpecificationService.checkSpecificationNameUnique(proSpecification))) {
            return error("修改规格'" + proSpecification.getSpecificationName() + "'失败，规格名称已存在");
        }
        return toAjax(proSpecificationService.updateProSpecification(proSpecification));
    }

    /**
     * 删除规格
     * @param ids
     * @return
     */
    @RequiresPermissions("product:proSpecification:remove")
    @Log(title = "产品规格管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(proSpecificationService.deleteProSpecificationByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
}
